/*
 * @Title: 票选详情
 * @port: 熊健
 * @Author: 徐兵
 * @Date:   2018-12-21
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-12-21 10:52:19
 */
var app = {},
  dataInfo = new Object(),
  module = {
    init: function() {
      app = new Vue({
        el: '#app',
        data: {
          openid: openId,
          aliPay: '',
          submitSwitch: 1,
          isWx: is_weixn(),
          tab: ["票选理由", "溯源"],
          active: 0,
          num: '75.00',
          voteDatailsData: {}, //详情数据
          Backsource: [],
          Length: "",
          currentPercent: '',
          relationId: "", //关系Id
          isApp: isApp == 1 ? 1 : 0,
          margintop: "",
          display: "",
          isGiftVip: !getQueryString('isGiftVip') ? 0 : getQueryString('isGiftVip'),
          img: ["../img/voteDetail/if_grandmother_3231123.png", "../img/voteDetail/if_grandmother_3231123.png", "../img/voteDetail/if_grandmother_3231123.png"],
          inviterId: !getQueryString("inviterId") || getQueryString('inviterId') == 'null' ? userId : getQueryString("inviterId")
        },
        methods: {
          init: function() {
            app.FXPTVoteinfo();
            app.getBacksource();
            shareInfo.voteItemCode = getQueryString('voteItemCode');
            app.active = getQueryString('active') ? getQueryString('active') : 0;
            !!getQueryString('inviterId') && getQueryString('inviterId') != 'null' ? app.margintop = "margin-top:52px" : "";
            !!getQueryString('inviterId') && getQueryString('inviterId') != 'null' ? app.display = "display:block" : app.display = "display:none";
          },
          goSource: function(item) { //溯源详情
            console.log(item)
            if (item.sourceType == 'text') {
              location.href = 'FPtSourceTalk.html?sourceCode=' + encodeURIComponent(item.sourceCode) + '&sourceTitle=' + encodeURIComponent(item.sourceTitle) + '&id=' + encodeURIComponent(item.id);
            } else if (item.sourceType == 'video') {
              location.href = 'FPtVoteVedioDetail.html?sourceCode=' + encodeURIComponent(item.sourceCode) + '&sourceTitle=' + encodeURIComponent(item.sourceTitle) + '&id=' + encodeURIComponent(item.id);
            }
          },
          toPay: function() {
            dataInfo.returnUrl = window.location.href.split('/html/')[0] + '/html/';
            dataInfo.returnUrl += 'FPtPayCall';
            dataInfo.returnUrl += '.html?orderId=' + this.relationId + '&voteItemCode=' + getQueryString('voteItemCode') + "&inviterId=" + this.inviterId;
            dataInfo.payInfo = new Object();
            dataInfo.payInfo.userid = userId;
            dataInfo.payInfo.relationId = this.relationId;
            if (app.isWx) {
              dataInfo.payInfo.payType = 'WxH5';
              dataInfo.payInfo.openid = this.openId
            } else {
              dataInfo.payInfo.returnUrl = dataInfo.returnUrl;
              dataInfo.payInfo.payType = 'AliH5';
            };

            if (!!this.isWx && (window.location.href.indexOf('mtmall.meetao.com') != 8)) { //微信支付跳转第三方
              //版本号1.1 没有拼团数据
              dataInfo.payType = 5;
              //1 - 普通支付（分销商品、大礼包）、 2 - 普通订单支付 、3 - 拼团支付 、 4 - 拼团订单支付
              //181128 - 增加灰度拼团跳转
              location.href = 'https://premtmall.meetao.com/html/WxPay.html?mallUrl=' + window.location.href.split('/html/')[0] + '&ajaxUrl=' + addressPayUrl + '&orderId=' + this.relationId + '&openid=' + this.openid + '&payType=' + dataInfo.payType + '&voteItemCode=' + getQueryString('voteItemCode') + '&userid=' + userId + "&inviterId=" + this.inviterId;
            } else {
              ajaxTemplate(addressPayUrl + SERVICE.pxSign, function(data) {
                console.log(data);
                if (data.tag == 0 && app.isWx) { //wxPay
                  dataInfo.init.wxInfo = data.data.result;
                  if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                      document.addEventListener('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result), false);
                    } else if (document.attachEvent) {
                      document.attachEvent('WeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                      document.attachEvent('onWeixinJSBridgeReady', dataInfo.init.onBridgeReady(data.data.result));
                    }
                  } else {
                    app.onBridgeReady(data.data.result);
                  };
                } else if (data.tag == 0 && !app.isWx) { //alipay
                  app.aliPay = decodeURIComponent(data.data.result.sign.replace(/\+/g, " "));
                  setTimeout(function() {
                    document.forms[0].submit();
                  }, 250);
                } else {
                  //pt外部分享支付 有可能存在无openid问题 支付不成功需打开此处注释
                  app.$toast.clear();
                  app.$toast(data.errMsg);
                };
                app.submitSwitch = 1;
              }, dataInfo.payInfo);
            }
          },
          onBridgeReady: function(msg) { //wxPay
            app.$toast.clear();
            WeixinJSBridge.invoke(
              'getBrandWCPayRequest', {
                "appId": msg.appId, //公众号名称，由商户传入
                "timeStamp": msg.timeStamp, //时间戳，自1970年以来的秒数
                "nonceStr": msg.nonceStr, //随机串
                "package": msg.package_str,
                "signType": "MD5", //微信签名方式：
                "paySign": msg.paySign //微信签名
              },
              function(res) {
                console.log(res);
                if (res.err_msg == "get_brand_wcpay_request:ok") { //微信支付成功 - 支付成功页面
                  location.href = dataInfo.returnUrl;
                } else { //微信支付失败跳转我的订单页面
                  location.href = window.location.href;
                };
              });
          },

          goShare: function() { //分享朋友
            if (app.voteDatailsData.mIsfirstIssueNum == 0) {
              location.href = "FPtShareDetails.html?activityIssueNum=" + app.voteDatailsData.voteActivityIssueNum
            } else if (app.voteDatailsData.mIsfirstIssueNum == 1) {
              location.href = 'FPtShareFriends.html?activityIssueNum=' + app.voteDatailsData.voteActivityIssueNum
            }
          },
          goPay: function() { //去支付
            if (this.submitSwitch == 1) {
              this.submitSwitch = 2;
              dataInfo.fxPTvoteData = {
                voteCode: getQueryString('voteItemCode'),
                voteGoodName: app.voteDatailsData.voteItemName,
                voteGoodshowName: app.voteDatailsData.showItemName,
                voteGoodUrl: app.voteDatailsData.MainImgUrl,
                userId: userId,
                Inviterid: getQueryString('inviterId')
              };
              if (getQueryString('inviterId') == 'null' || !getQueryString('inviterId')) {
                delete dataInfo.fxPTvoteData.Inviterid
              };
              ajaxTemplate(addressTest + SERVICE.FXPTvote, function(data) {
                if (data.tag == 0) { //熊哥投票成功回调
                  app.relationId = data.data.result;
                  app.toPay();
                } else {
                  app.$toast(data.errMsg)
                  app.submitSwitch = 1;
                };
              }, dataInfo.fxPTvoteData);
            }
          },
          left: function(item, index, event) {
            app.Length = parseInt(100 / (app.voteDatailsData.pricelist.length))
            return "left:" + app.Length * index + "%"
          },
          Mleft: function(num) {
            app.currentPercent = ((Number(app.voteDatailsData.showNumMan) - Number(app.voteDatailsData.pricelist[num].bottomVoteNum)) / (Number((Number(num) + 1 == app.voteDatailsData.pricelist.length) ? ((Number(app.voteDatailsData.showNumMan) - app.voteDatailsData.pricelist[num].bottomVoteNum)) * 2 : app.voteDatailsData.pricelist[num].topVoteNum - app.voteDatailsData.pricelist[num].bottomVoteNum)) * app.Length) + parseInt(app.Length * num)
            setTimeout(function() {
              $(".voteNumber").css("margin-left", "-" + $(".voteNumber").outerWidth() / 2 + "px")
            }, 100)
            return "left:" + app.currentPercent + "%"
          },
          bar: function(num) { //偏移量
            return "width:" + app.currentPercent + "%"
          },
          FXPTVoteinfo: function() { //票选详情

            ajaxTemplate(addressTest + SERVICE.FXPTVoteinfo, function(data) {
              if (data.tag == 0) {
                app.voteDatailsData = data.data.result;
                if (getQueryString('inviterId')) { //分享次数
                  ajaxTemplate(addressTest + SERVICE.FXPTaddClickLog, function(data) {
                    if (data.tag == 0) {} else {};
                  }, {
                    inviterId: getQueryString('inviterId'),
                    activityIssueNum: data.data.result.voteActivityIssueNum,
                    itemCode: getQueryString('voteItemCode'),
                  });
                }
              } else {
                app.$toast(data.errMsg)
              };
            }, {
              voteItemCode: getQueryString('voteItemCode'),
              userId: userId
            });

          },
          getBacksource: function() { //溯源列表
            ajaxTemplate(addressTest + SERVICE.backsource, function(data) {
              if (data.tag == 0) {
                app.Backsource = data.data.result;
              } else {
                app.$toast(data.errMsg)
              };
            }, {
              page: 1,
              limit: 10000,
              type: 1,
              voteItemCode: getQueryString('voteItemCode')
            });
          },
          getCurrentPrice: function(num) { //获取当前价
            for (var i in app.voteDatailsData.pricelist) {

              if (num >= app.voteDatailsData.pricelist[i].bottomVoteNum && num < app.voteDatailsData.pricelist[i].topVoteNum) {
                return i
              }
            }
          }
        }

      });
      app.init();
    },
  }
module.init();
